﻿using System;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
using System.Linq;
using System.IO;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Pdf
{
    public class PdfRenumbering
    {
        private string _docBaseName;
        private Document doc;
        private PdfFonts BookFonts;
        private BookConfiguration bookConfig;
        
        
        public PdfRenumbering(string footerText, PdfFonts fonts, BookConfiguration cfg)
        {
            _docBaseName = footerText;
            BookFonts = fonts;
            bookConfig = cfg;
        }

        public void AddPageNumbers(string pdfSourcePath, string pdfDestPath)
        {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(pdfSourcePath);

            // we retrieve the total number of pages
            int n = reader.NumberOfPages;

            // we retrieve the size of the first page
            Rectangle psize = reader.GetPageSize(1);

            // step 1: creation of a document-object
            doc = new Document(psize, bookConfig.Margins.Left, bookConfig.Margins.Right,
                bookConfig.Margins.Top, bookConfig.Margins.Bottom);

            // step 2: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdfDestPath, FileMode.Create));

            doc.Open();
            
            // dd content
            PdfContentByte cb = writer.DirectContent;

            int p = 0;
            for (int page = 1; page <= reader.NumberOfPages; page++)
            {
                doc.NewPage();
                p++;

                PdfImportedPage importedPage = writer.GetImportedPage(reader, page);
                cb.AddTemplate(importedPage, 0, 0);
                if (page > 2)
                {
                    PdfPTable tbFooter = FooterTable(_docBaseName, page - 2);

                    tbFooter.WriteSelectedRows(0, -1, 2*bookConfig.Margins.Left, bookConfig.Margins.Top, writer.DirectContent);
                }

            }
            // step 5: we close the document
            doc.Close();
        }


        public PdfPTable FooterTable(string title, int pageNr)
        {
            PdfPTable table = new PdfPTable(2);
            float[] widths = new float[] { 50f, 50f };
            table.TotalWidth = doc.PageSize.Width - (2 * (bookConfig.Margins.Left + bookConfig.Margins.Right));


            // We invert text and page number according to page generated
            string left = pageNr % 2 == 0 ? pageNr.ToString() : title;
            string right = pageNr % 2 == 0 ? title : pageNr.ToString();
            Font leftFont = pageNr % 2 == 0 ? BookFonts.FontImpressumTitle : BookFonts.FontImpressumValue;
            Font rightFont = pageNr % 2 == 0 ? BookFonts.FontImpressumValue : BookFonts.FontImpressumTitle;

            // 1st cell is number if even, else user defined bottom text
            Phrase p = new Phrase(left, leftFont);
            PdfPCell cell = new PdfPCell(p);
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            // 2nd cell is number if off, else user defined bottom text
            p = new Phrase(right, rightFont);
            cell = new PdfPCell(p);
            cell.Border = Rectangle.NO_BORDER;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            table.AddCell(cell);

            return table;
        }
    }
}
